//
//  GXScrollView.m
//  SXH
//
//  Created by 龚啸 on 15/10/27.
//  Copyright © 2015年 lyss. All rights reserved.
//

#import "GXScrollView.h"
#define KFONT 12.0
#define KPOSISTION 15
#define KDISTANCE 2
#define KORIGINY 11
#define KHEIGHT 35
#import "GXScrollEntity.h"
@implementation GXScrollView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithEntityArray:(NSMutableArray *)array
{
    self = [super init];
    if (self) {
        self.arrayList = array;
        float width = 0.0;
        [self setShowsHorizontalScrollIndicator:NO];
        self.bounces = YES;
        for (int i=0; i< [array count]; i++) {
            GXScrollEntity *entity = [array objectAtIndex:i];
            CGSize buttonSize = [entity.name sizeWithFont:[UIFont systemFontOfSize:KFONT]];
            
            UIButton *btn =[[UIButton alloc] init];
            btn.tag = [entity.type intValue];
            
            [btn setTitle:entity.name forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:KFONT];
            btn.frame = CGRectMake(width + KDISTANCE, 0, buttonSize.width + KPOSISTION,KHEIGHT);
            width = CGRectGetMaxX(btn.frame);
            UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(btn.frame), KORIGINY, 1, 15)];
            label.backgroundColor = RGBCOLOR(238, 238, 238);
            [self addSubview:btn];
            if (i != [array count] -1) {
                [self addSubview:label];
            }else{
                float k = CGRectGetMaxX(btn.frame);
                self.contentSize = CGSizeMake(k, KHEIGHT);
                
                if (k + 10 < SCREEN_WIDTH - 95) {
                    self.frame = CGRectMake(4, 3,k, KHEIGHT);
                } else {
                    self.frame = CGRectMake(4, 3,SCREEN_WIDTH - 105 ,KHEIGHT);
                }
            }
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return self;
}
-(void)btnClick:(id)sender
{
    
    for (int i = 0; i< [self.arrayList count] ; i++) {
         GXScrollEntity *entity = [self.arrayList objectAtIndex:i];
        UIButton * btn = nil;
        btn = (UIButton *)[self viewWithTag:[entity.type intValue]];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    }
    UIButton *btn2 = (UIButton *)sender;
    [btn2 setTitleColor:[UIColor colorWithRed:0.32 green:0.77 blue:0.24 alpha:1] forState:UIControlStateNormal];
    if ([self.gxDelegate respondsToSelector:@selector(gxScrollView:ActionWithIndex:)]) {
        [self.gxDelegate  gxScrollView:self ActionWithIndex:btn2.tag];
    }
    
}
-(void)clearColor
{
    for (int i = 0; i< ([self.arrayList count]); i++) {
        GXScrollEntity *entity = [self.arrayList objectAtIndex:i];
        UIButton * btn = nil;
        btn = (UIButton *)[self viewWithTag:[entity.type intValue]];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    }

}

@end
